﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/28 星期四 23:10:32
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 数据分组访问对象
    /// </summary>
    public class DataGroupDAO : DAOBase<S7_DataGroup>, IDataGroupDAO
    {
        #region Public functions
        /// <summary>
        /// 添加数据分组
        /// </summary>
        /// <param name="group">数据分组</param>
        /// <returns>数据分组编号</returns>
        public override object Create(S7_DataGroup group)
        {
            string sql = @"
                INSERT INTO S7_DataGroup
                        (GroupIdentifier
                        ,GroupName
                        ,GroupType
                        ,MajorVersion
                        ,MinorVersion
                        ,RevisionNumber)
                    VALUES
                        (@GroupIdentifier
                        ,@GroupName
                        ,@GroupType
                        ,@MajorVersion
                        ,@MinorVersion
                        ,@RevisionNumber)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                        DBUtil.CreateSqlParameter(group.GroupIdentifier, "GroupIdentifier"),
                        DBUtil.CreateSqlParameter(group.GroupName, "GroupName"),
                        DBUtil.CreateSqlParameter(group.GroupType, "GroupType"),
                        DBUtil.CreateSqlParameter(group.MajorVersion, "MajorVersion"),
                        DBUtil.CreateSqlParameter(group.MinorVersion, "MinorVersion"),
                        DBUtil.CreateSqlParameter(group.RevisionNumber, "RevisionNumber"));
            }
        }

        /// <summary>
        /// 根据分组标识符读取数据分组
        /// </summary>
        /// <param name="groupIdentifier">分组标识符</param>
        /// <returns>数据分组</returns>
        public S7_DataGroup Read(string groupIdentifier)
        {
            object dataGroup = null;

            string sql = @"
                SELECT *
                    FROM S7_DataGroup
                WHERE GroupIdentifier = @GroupIdentifier";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(groupIdentifier, "GroupIdentifier"));

                if (reader.Read())
                {
                    dataGroup = new S7_DataGroup();
                    DBUtil.ReadData(reader, ref dataGroup);
                }
            }

            return dataGroup as S7_DataGroup;
        }

        /// <summary>
        /// 读取所有数据分组
        /// </summary>
        /// <returns>数据分组列表</returns>
        public override List<S7_DataGroup> ReadAll()
        {
            List<S7_DataGroup> groups = new List<S7_DataGroup>();

            string sql = @"
                SELECT *
                    FROM S7_DataGroup
                ORDER BY GroupIdentifier";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql);

                while (reader.Read())
                {
                    object group = new S7_DataGroup();

                    DBUtil.ReadData(reader, ref group);

                    groups.Add(group as S7_DataGroup);
                }
            }

            return groups;
        }

        /// <summary>
        /// 根据分组类型读取所有数据分组
        /// </summary>
        /// <param name="groupType">数据分组类型</param>
        /// <returns>数据分组列表</returns>
        public List<S7_DataGroup> ReadAll(int groupType)
        {
            List<S7_DataGroup> groups = new List<S7_DataGroup>();

            string sql = @"
                    SELECT *
                        FROM S7_DataGroup
                    WHERE
                        GroupType = @GroupType
                    ORDER BY GroupIdentifier";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(groupType, "GroupType"));

                while (reader.Read())
                {
                    object group = new S7_DataGroup();
                    DBUtil.ReadData(reader, ref group);

                    groups.Add(group as S7_DataGroup);
                }
            }

            return groups;
        }
        #endregion
    }
}
